package com.chima.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;
import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.chima.domain.DepartmentCoffer;
import com.chima.domain.Employees;
import com.chima.domain.Department;
import com.chima.domain.PageRequestBean;
import com.chima.domain.PageResponseBean;
import com.chima.domain.Position;
import com.chima.service.DepartmentFinaceService;
import com.chima.service.DepartmentService;
import com.chima.service.DetailJobService;
import com.chima.service.DetailPostService;
import com.chima.service.EmployeeService;
import com.chima.service.PerformanceService;
import com.chima.service.PostCompetentService;
import com.chima.service.ProfessionalService;
import com.chima.service.RateService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import flexjson.JSONSerializer;
public class EmployeesAction extends ActionSupport implements ModelDriven<Employees> {
    Employees employee =new Employees();
    public Employees getModel() {
    	return employee;
    }
	private int start;// 开始页码
	private int limit; //每页显示
	private int zu; //每页显示
	public int getZu() {
		return zu;
	}
	public void setZu(int zu) {
		this.zu = zu;
	}
	private int page;//页数
	private  int  deptId;
	public int getDeptId() {
		return deptId;
	}
	public void setDeptId(int deptId) {
		this.deptId = deptId;
	}
	private  EmployeeService employeeService;
	private  DepartmentService departmentService;
	private  PostCompetentService postCompetentService;
	private  ProfessionalService  professionalService ;
	private  DepartmentFinaceService deptFinaceService;
	private DetailPostService detailPostService;
	private RateService  rateService;
	private PerformanceService performanceService ;
	private DetailJobService detailJobService;
	
	private List<Employees> employees;
	private boolean success=false ;
	private String param;
	private int param1; //根据科室查找人员
	private File upload;
	private String uploadContentType;
	private String uploadFileName;
	private int roseID; //角色
	private int empID;
	private int [] depar;
	private String workage;
	private int level;
	private int serchdepartment;
	private double finaceCoffe;
	private String dept;
	private String profession;
	private double coffer;
	private int id;
	

	public DetailJobService getDetailJobService() {
		return detailJobService;
	}
	public void setDetailJobService(DetailJobService detailJobService) {
		this.detailJobService = detailJobService;
	}
	public PerformanceService getPerformanceService() {
		return performanceService;
	}
	public void setPerformanceService(PerformanceService performanceService) {
		this.performanceService = performanceService;
	}
public DetailPostService getDetailPostService() {
		return detailPostService;
	}
	public void setDetailPostService(DetailPostService detailPostService) {
		this.detailPostService = detailPostService;
	}
public RateService getRateService() {
		return rateService;
	}
	public void setRateService(RateService rateService) {
		this.rateService = rateService;
	}
public double getCoffer() {
		return coffer;
	}
	public void setCoffer(double coffer) {
		this.coffer = coffer;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
public DepartmentFinaceService getDeptFinaceService() {
		return deptFinaceService;
	}
	public void setDeptFinaceService(DepartmentFinaceService deptFinaceService) {
		this.deptFinaceService = deptFinaceService;
	}
public String getDept() {
		return dept;
	}
	public void setDept(String dept) {
		this.dept = dept;
	}
	public String getProfession() {
		return profession;
	}
	public void setProfession(String profession) {
		this.profession = profession;
	}

public double getFinaceCoffe() {
		return finaceCoffe;
	}
	public void setFinaceCoffe(double finaceCoffe) {
		this.finaceCoffe = finaceCoffe;
	}
public int getSerchdepartment() {
		return serchdepartment;
	}
	public void setSerchdepartment(int serchdepartment) {
		this.serchdepartment = serchdepartment;
	}
public int getLevel() {
		return level;
	}
	public void setLevel(int level) {
		this.level = level;
	}
public String getWorkage() {
		return workage;
	}
	public void setWorkage(String workage) {
		this.workage = workage;
	}
public ProfessionalService getProfessionalService() {
		return professionalService;
	}
	public void setProfessionalService(ProfessionalService professionalService) {
		this.professionalService = professionalService;
	}
public int[] getDepar() {
		return depar;
	}
	public void setDepar(int[] depar) {
		this.depar = depar;
	}
public PostCompetentService getPostCompetentService() {
		return postCompetentService;
	}
	public void setPostCompetentService(PostCompetentService postCompetentService) {
		this.postCompetentService = postCompetentService;
	}
public int getParam1() {
		return param1;
	}
	public void setParam1(int param1) {
		this.param1 = param1;
	}
public int getEmpID() {
		return empID;
	}
	public void setEmpID(int empID) {
		this.empID = empID;
	}
public int getRoseID() {
		return roseID;
	}
	public void setRoseID(int roseID) {
		this.roseID = roseID;
	}
public List<Employees> getEmployees() {
		return employees;
	}
	public void setEmployees(List<Employees> employees) {
		this.employees = employees;
	}
public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
public DepartmentService getDepartmentService() {
		return departmentService;
	}
	public void setDepartmentService(DepartmentService departmentService) {
		this.departmentService = departmentService;
	}
public String getParam() {
		return param;
	}
	public void setParam(String param) {
		this.param = param;
	}
public boolean isSuccess() {
		return success;
	}
	public void setSuccess(boolean success) {
		this.success = success;
	}
public EmployeeService getEmployeeService() {
		return employeeService;
	}
	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}
	
public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
public String  ListEmployees() throws Exception{
	    PageRequestBean pageRequestBean = new PageRequestBean();
	    DetachedCriteria criteria = DetachedCriteria.forClass(Employees.class);
//		if(param!=null&&param.trim().length()>0){ 
//			Department d=departmentService.findByName(param);
//			criteria.add(Restrictions.eq("department",d));
//		}
//		if(param1!=0){
//			criteria.add(Restrictions.eq("department.id",param1));
//		}
	    Employees  existEmployee =(Employees) ServletActionContext.getRequest().getSession().getAttribute("existEmployee");
	    if(!existEmployee.getName().equals("admin")){
	    	criteria.add(Restrictions.eq("department.id",existEmployee.getDepartment().getId()));
	    	pageRequestBean.setDept(existEmployee.getDepartment().getId());
	    }else{
	    	if(serchdepartment!=0){
				criteria.add(Restrictions.eq("department.id",serchdepartment));
				pageRequestBean.setDept(serchdepartment);
			}
	    }
		
//		criteria.add(Restrictions.isNotNull("department.id"));
		pageRequestBean.setCriteria(criteria);
		pageRequestBean.setStart(start);
		pageRequestBean.setLimit(limit);
		pageRequestBean.setPage(page);
		PageResponseBean pageResponseBean =employeeService.findEmployee(pageRequestBean);
        employees=pageResponseBean.getRows();
        int total=0;
        total =pageResponseBean.getTotal();
		// 输出json内容到客户端
        JSONSerializer jsonSerializer = new JSONSerializer();
        jsonSerializer.include("*.id","*.name","*.professional","*.department","*.workage","*.education","*.password","*.card");
        jsonSerializer.exclude("*.class");
        String resultJson1 = jsonSerializer.deepSerialize(employees);  
        String resultJson2 =jsonSerializer.serialize(total);
        System.out.println(resultJson2);
        String jsonString = "{total:"+resultJson2.toString()+",employees:"+resultJson1.toString()+"}";
        System.out.print(jsonString);
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
  }
public String addEmployees() throws Exception{
	        ServletContext servletContext = ServletActionContext.getServletContext();
			String uploadDir = servletContext.getRealPath("/upload");
			String url=null;
			// 写文件
			   if(upload!=null){
					SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
					String someFileName = sdf1.format(new Date()) + uploadFileName;
				    url="upload/"+someFileName;
					InputStream in = new BufferedInputStream(new FileInputStream(upload));
					OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(uploadDir,someFileName)));// 真实文件名
					int b;
					while((b= in.read())!=-1){
						out.write(b);
					}
					in.close();
					out.close(); 
	         }
		      Department depart=departmentService.findById(employee.getDepartment().getId());
			   Employees e=new Employees();
			   //岗位胜任系数查询
			   Employees ee=new Employees();
			   ee.setEducation(employee.getEducation());
			   ee.setProfessional(professionalService.findById(employee.getProfessional().getId()));
			   ee.setWorkage(employee.getWorkage());
			   ee.setZhiban(employee.getZhiban());
			  
			   double  postCompetent=postCompetentService.findCoffer(ee);
			   e.setName(employee.getName());
			   e.setPassword(employee.getPassword());
			   if(url!=null){
				   e.setPhotoUrl(url);
			   }
			   e.setCard(employee.getCard());
			   e.setDepartment(depart);
			   e.setPostManager(employee.getPostManager());
			   e.setEducation(employee.getEducation());
			   e.setProfessional(employee.getProfessional());
			   e.setWorkage(employee.getWorkage());
			   e.setPostCompetent(postCompetent);
			   e.setLevel(1);
			   e.setFinaceCoffe(employee.getFinaceCoffe());
			   // 修改等级副主任
			   if(employee.getPosition()==4){
				   e.setPosition(2);
			   }else{
				   e.setPosition(employee.getPosition());
			   }
			   e.setPostcoffe(employee.getPostcoffe());
			   e.setZhiban(employee.getZhiban());
			   e.setLevel(0);
			   employeeService.saveEmployees(e);
			   // 更新
//			   DepartmentCoffer d=deptFinaceService.FindDepartmentCofferByName(depart.getName());
//			   if(d!=null){
//				   double  oldcoffer=d.getCoffe();
//				   double newcoffe=oldcoffer+employee.getFinaceCoffe();
//				   d.setCoffe(newcoffe);
//				   deptFinaceService.updateDepartmentCoffer(d);
//			   }
			   
			this.success=true;
			JSONSerializer jsonSerializer = new JSONSerializer();
			String resultJson =jsonSerializer.serialize(success);
			String jsonString = "{success:"+resultJson.toString()+"}";
			ServletActionContext.getResponse().setContentType("text;charset=utf-8");
			ServletActionContext.getResponse().getWriter().print(jsonString);
	return NONE;
}
public void editEmployees() throws Exception{
	
	    ServletContext servletContext = ServletActionContext.getServletContext();
		String uploadDir = servletContext.getRealPath("/upload");
		// 写文件
		String url=null;
		if(upload!=null){
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
			String someFileName = sdf1.format(new Date()) + uploadFileName;
			url="upload/"+someFileName;
			InputStream in = new BufferedInputStream(new FileInputStream(upload));
			OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(uploadDir,someFileName)));// 真实文件名
			int b;
			while((b= in.read())!=-1){
				out.write(b);
			}
			in.close();
			out.close(); 
		}
		  // Department depart=departmentService.findByName(dept);
		   Employees e=employeeService.findEmployeesById(employee.getId());
		  // Department olddepart=e.getDepartment();
		   // 查询胜任系数
		   Employees ee=new Employees();
		   
		   //double  oldfinace=e.getFinaceCoffe();
		   
		   ee.setEducation(employee.getEducation());
		   ee.setProfessional(professionalService.findByName(profession));
		   ee.setWorkage(employee.getWorkage());
		   ee.setZhiban(employee.getZhiban());
		   System.out.println("employee身份证："+employee.getCard());
		   e.setCard(employee.getCard());
		   double  postCompetent=postCompetentService.findCoffer(ee);
		  
		   e.setPostCompetent(postCompetent);
		   e.setFinaceCoffe(employee.getFinaceCoffe());
		   e.setZhiban(employee.getZhiban());
		   e.setName(employee.getName());
		   e.setPassword(employee.getPassword());
		   if(url!=null){
			   e.setPhotoUrl(url);
		   }
		// 修改等级副主任
		   if(employee.getPosition()==4){
			   e.setPosition(2);
		   }else{
			   e.setPosition(employee.getPosition());
		   }
		   e.setDepartment(departmentService.findByName(dept));
		   e.setPostManager(employee.getPostManager());
		   e.setEducation(employee.getEducation());
		   e.setProfessional(professionalService.findByName(profession));
		   e.setPostcoffe(employee.getPostcoffe());
		   e.setWorkage(employee.getWorkage());
		   e.setLevel(1);
		   employeeService.updateEmployees(e);
		   
//		   DepartmentCoffer newd1=deptFinaceService.FindDepartmentCofferByName(dept);
//		   
//		   DepartmentCoffer oldd2=deptFinaceService.FindDepartmentCofferByName(olddepart.getName());
//		   if(!olddepart.getName().endsWith(dept)){
//			   //更新原来科室存量
//			   if(oldd2!=null){
//				   
//				   System.out.println("原来奖金系===============================数是："+oldfinace);
//				   double  oldcoffer1=oldd2.getCoffe();
//				   double newcoffe1=oldcoffer1-oldfinace;;
//				   oldd2.setCoffe(newcoffe1);
//				   System.out.println("减少后奖金系===============================数是："+newcoffe1);
//				   deptFinaceService.updateDepartmentCoffer(oldd2);
//			   }
//			   // 更新更改后的科室存量
//			   if(newd1!=null){
//				   double  oldcoffer=newd1.getCoffe();
//				   double newcoffe=oldcoffer+employee.getFinaceCoffe();
//				   newd1.setCoffe(newcoffe);
//				   deptFinaceService.updateDepartmentCoffer(newd1);
//			   }
//		   }
		  
			this.success=true;
			JSONSerializer jsonSerializer = new JSONSerializer();
			String resultJson =jsonSerializer.serialize(success);
			String jsonString = "{success:"+resultJson.toString()+"}";
			ServletActionContext.getResponse().setContentType("text;charset=utf-8");
			ServletActionContext.getResponse().getWriter().print(jsonString);
		}
	public String deleteEmployees(){
		 Employees e=employeeService.findEmployeesById(employee.getId());
		 Department depart=e.getDepartment();
		// double  empoldcoffer=e.getFinaceCoffe();
		 DepartmentCoffer d=deptFinaceService.FindDepartmentCofferByName(depart.getName());
		
		  rateService.deleterate(employee.getId());
		  detailJobService.deletDetailJobByemp(employee.getId());
		  detailPostService.deleteDetailPostByEmp(employee.getId());
		  performanceService.DeletePerformance(employee.getId());
		  employeeService.deleteEmployees(employee.getId());
//		  if(d!=null){
//			  if(d.getCoffe()>((d.getCoffe()-empoldcoffer)*1.1)){
//				  double newcoffer=(d.getCoffe()-empoldcoffer)*1.1;
//				  d.setCoffe(newcoffer);
//				  deptFinaceService.updateDepartmentCoffer(d);
//			  }
//		  }
			return NONE;
		}
public String  ListEmp() throws Exception{
	// 输出json内容到客户端
	Employees  e =(Employees) ServletActionContext.getRequest().getSession().getAttribute("existEmployee");
    JSONSerializer jsonSerializer = new JSONSerializer();
    
    jsonSerializer.exclude("*.class","*.jobDetails","*.records","*.jobs","*.employees","*.weights");
    String resultJson1 = jsonSerializer.deepSerialize(e);  
    String jsonString = "{employees:"+resultJson1.toString()+"}";
    System.out.println("要完成任务的 =======================人信息是："+jsonString);
	ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
	ServletActionContext.getResponse().getWriter().print(jsonString);
	return NONE;
}
public String  editEmppassword() throws Exception{
	
    PageRequestBean pageRequestBean = new PageRequestBean();
    DetachedCriteria criteria = DetachedCriteria.forClass(Employees.class);
    Employees  existEmployee =(Employees) ServletActionContext.getRequest().getSession().getAttribute("existEmployee");
    if(existEmployee.getName().equals("admin")){
    	if(serchdepartment!=0){
			criteria.add(Restrictions.eq("department.id",serchdepartment));
			pageRequestBean.setDept(serchdepartment);	 
		}
    	pageRequestBean.setCriteria(criteria);
		pageRequestBean.setStart(start);
		pageRequestBean.setLimit(limit);
		pageRequestBean.setPage(page);
		PageResponseBean pageResponseBean =employeeService.findEmployee(pageRequestBean);
	    employees=pageResponseBean.getRows();
	    int total=0;
	    total =pageResponseBean.getTotal(); 
    	// 输出json内容到客户端
        JSONSerializer jsonSerializer = new JSONSerializer();
        jsonSerializer.include("*.id","*.name","*.professional","*.department","*.workage","*.education","*.password");
        jsonSerializer.exclude("*.class");
        String resultJson1 = jsonSerializer.deepSerialize(employees);  
        String resultJson2 =jsonSerializer.serialize(total);
        String jsonString = "{total:"+resultJson2.toString()+",employees:"+resultJson1.toString()+"}";
    	ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
    	ServletActionContext.getResponse().getWriter().print(jsonString);
    }else{
    	// 输出json内容到客户端
    	Employees  emp=(Employees) ServletActionContext.getRequest().getSession().getAttribute("existEmployee");
    	Employees  e=employeeService.findEmployeesById(emp.getId());
        JSONSerializer jsonSerializer = new JSONSerializer();
        jsonSerializer.exclude("*.class");
        String resultJson1 = jsonSerializer.deepSerialize(e);  
        String jsonString = "{employees:"+resultJson1.toString()+"}";
    	ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
    	ServletActionContext.getResponse().getWriter().print(jsonString);
    	
    }
	
	return NONE;
}
public String addRole() throws Exception{
//	    List<Employees> EMP=new ArrayList<Employees>();
//	    for( int i=0; i<depar.length;i++){
//	    	List<Employees> E=employeeService.findEmployeesByDeparmet(depar[i]);
//	    	for(Employees e:E){
//	    		EMP.add(e);
//	    	}
//	    } 
//	    String[] dd=new String [EMP.size()]; 
//	    for(int k=0; k<EMP.size();k++){
//	    	dd[k]=String.valueOf(EMP.get(k).getId());	
//	    }
//	    employeeService.addRole(empID, roseID,getStr(dd));
	    employeeService.addEMPRole(empID, roseID);
	 	this.success=true;
		JSONSerializer jsonSerializer = new JSONSerializer();
		String resultJson =jsonSerializer.serialize(success);
		String jsonString = "{success:"+resultJson.toString()+"}";
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
	}
public String  personEmp() throws Exception{
	// 输出json内容到客户端
	Employees  e =(Employees) ServletActionContext.getRequest().getSession().getAttribute("existEmployee");
	Map<String, Object> m = new HashMap<String, Object>();
	if(e.getName().equals("admin")){
		m.put("perfession","无");
		m.put("name", e.getName());
		m.put("url", e.getPhotoUrl());
		m.put("department","无");
	}else {
		m.put("perfession",e.getProfessional().getName());
		m.put("name", e.getName());
		m.put("url", e.getPhotoUrl());
		m.put("department", e.getDepartment().getName());
	}
	
	
	JsonConfig jsonConfig = new JsonConfig();
	JSONArray jsonArray = JSONArray.fromObject(m,jsonConfig);
	String jsonString = "{datas:"+jsonArray.toString()+"}";
	ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
	ServletActionContext.getResponse().getWriter().print(jsonString);
	return NONE;
}
public String  ListEmps() throws Exception{
    employees=employeeService.findEmployeesByDeparmet(param1);
	// 输出json内容到客户端

    JSONSerializer jsonSerializer = new JSONSerializer();
    jsonSerializer.exclude("*.class");
    int total=employees.size();
    String resultJson1 = jsonSerializer.deepSerialize(employees);  
    
    String resultJson2 =jsonSerializer.serialize(total);
  
    String jsonString = "{total:"+resultJson2.toString()+",employees:"+resultJson1.toString()+"}";
//    String jsonString = "{employees:"+resultJson1.toString()+"}";
	ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
	ServletActionContext.getResponse().getWriter().print(jsonString);
	return NONE;
}
public String  ListEmpByDepart() throws Exception{
	
	 Department d=departmentService.findByName(param);
    employees=employeeService.findEmployeesByDeparmet(d.getId());
	// 输出json内容到客户端

    JSONSerializer jsonSerializer = new JSONSerializer();
    jsonSerializer.exclude("*.class");
    int total=employees.size();
    String resultJson1 = jsonSerializer.deepSerialize(employees);  
    
    String resultJson2 =jsonSerializer.serialize(total);
  
    String jsonString = "{total:"+resultJson2.toString()+",employees:"+resultJson1.toString()+"}";
//    String jsonString = "{employees:"+resultJson1.toString()+"}";
	ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
	ServletActionContext.getResponse().getWriter().print(jsonString);
	return NONE;
}
public String  personName() throws Exception{
	// 输出json内容到客户端
	Employees  e =(Employees) ServletActionContext.getRequest().getSession().getAttribute("existEmployee");
  
	String name=e.getName();
	JSONSerializer jsonSerializer = new JSONSerializer();
	String resultJson =jsonSerializer.serialize(name);
	String jsonString = "{name:"+resultJson.toString()+"}";
	ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
	ServletActionContext.getResponse().getWriter().print(jsonString);
	return NONE;
}
	public String getStr(String[]args){
		String str="";
		for(int i=0;i<args.length;i++){ 
			if(i<args.length-1){
				str+=(String)args[i]+",";
			}else {
				str+=(String)args[i];
			}
	         
		}	
		return str;
	}
	public void editpassword() throws Exception{
		
	    ServletContext servletContext = ServletActionContext.getServletContext();
		String uploadDir = servletContext.getRealPath("/upload");
		// 写文件
		String url=null;
		if(upload!=null){
			SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
			String someFileName = sdf1.format(new Date()) + uploadFileName;
			 url="upload/"+someFileName;
			InputStream in = new BufferedInputStream(new FileInputStream(upload));
			OutputStream out = new BufferedOutputStream(new FileOutputStream(new File(uploadDir,someFileName)));// 真实文件名
			int b;
			while((b= in.read())!=-1){
				out.write(b);
			}
			in.close();
			out.close(); 
		}
		
	 try {
		    Employees e=employeeService.findEmployeesById(employee.getId());
		       e.setPassword(employee.getPassword());
			   if(url!=null){
				   e.setPhotoUrl(url);
			   }
		    employeeService.updateEmployees(e);
			} catch (Exception e) {
				e.printStackTrace();
			}	

				this.success=true;
				JSONSerializer jsonSerializer = new JSONSerializer();
				String resultJson =jsonSerializer.serialize(success);
				String jsonString = "{success:"+resultJson.toString()+"}";
				ServletActionContext.getResponse().setContentType("text;charset=utf-8");
				ServletActionContext.getResponse().getWriter().print(jsonString);
		}
	public String  ListleaderEmp() throws Exception{
		
	   Department d=departmentService.findByName("院领导");
	   employees=employeeService.findEmployeesByDeparmet(d.getId());
		// 输出json内容到客户端
	
	   JSONSerializer jsonSerializer = new JSONSerializer();
	   jsonSerializer.exclude("*.class");
	   int total=employees.size();
	   String resultJson1 = jsonSerializer.deepSerialize(employees);  
	   String resultJson2 =jsonSerializer.serialize(total);
	   String jsonString = "{total:"+resultJson2.toString()+",employees:"+resultJson1.toString()+"}";
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
	}
	/**
	 * 显示中层领导
	 * @return
	 * @throws Exception
	 */
	public String  ListMidderEmp() throws Exception{
		
		   employees=employeeService.findEmployeesByposition(2);
			// 输出json内容到客户端
		
		   JSONSerializer jsonSerializer = new JSONSerializer();
		   jsonSerializer.exclude("*.class");
		   int total=employees.size();
		   String resultJson1 = jsonSerializer.deepSerialize(employees);  
		   String resultJson2 =jsonSerializer.serialize(total);
		   String jsonString = "{total:"+resultJson2.toString()+",employees:"+resultJson1.toString()+"}";
			ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
			ServletActionContext.getResponse().getWriter().print(jsonString);
			return NONE;
		}
	
	/**
	 * 修改员工奖金系数
	 * 
	 * 
	 */
	public String  editEmcoffer() throws Exception{
		// 输出json内容到客户端
		Employees  e=employeeService.findEmployeesById(employee.getId());
		if(e!=null){
			e.setFinaceCoffe(employee.getFinaceCoffe());
			e.setPostManager(employee.getFinaceCoffe());
			employeeService.updateEmployees(e);
			this.success=true;
			JSONSerializer jsonSerializer = new JSONSerializer();
			String resultJson =jsonSerializer.serialize(success);
			String jsonString = "{success:"+resultJson.toString()+"}";
			ServletActionContext.getResponse().setContentType("text;charset=utf-8");
			ServletActionContext.getResponse().getWriter().print(jsonString);
		}
	   
		return NONE;
	}
/**
 * 显示普通员工各个系数
 *
 * 
 */
	public String  ListGeneralEmployees() throws Exception{
	    PageRequestBean pageRequestBean = new PageRequestBean();
	    DetachedCriteria criteria = DetachedCriteria.forClass(Employees.class);
	    if(serchdepartment!=0){
				criteria.add(Restrictions.eq("department.id",serchdepartment));
				pageRequestBean.setDept(serchdepartment);
		}
	    criteria.add(Restrictions.eq("position",1));
		pageRequestBean.setCriteria(criteria);
		pageRequestBean.setStart(start);
		pageRequestBean.setLimit(limit);
		pageRequestBean.setPage(page);
		PageResponseBean pageResponseBean =employeeService.findGeneralEmployee(pageRequestBean);
        employees=pageResponseBean.getRows();
        int total=0;
        total =pageResponseBean.getTotal();
		// 输出json内容到客户端
        JSONSerializer jsonSerializer = new JSONSerializer();
 
        jsonSerializer.exclude("*.class");
        String resultJson1 = jsonSerializer.deepSerialize(employees);  
        String resultJson2 =jsonSerializer.serialize(total);
        String jsonString = "{total:"+resultJson2.toString()+",employees:"+resultJson1.toString()+"}";
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
  }
	/**
	 * 修改员工系数
	 */
	public String  editGeneralEmp() throws Exception{
		
		Employees  e=employeeService.findEmployeesById(employee.getId());
		if(e!=null){
			e.setFinaceCoffe(employee.getFinaceCoffe());
			e.setPostcoffe(employee.getPostcoffe());
			e.setPostManager(employee.getPostManager());
//			e.setPostCompetent(employee.getPostCompetent());
			employeeService.updateEmployees(e);
			this.success=true;
			JSONSerializer jsonSerializer = new JSONSerializer();
			String resultJson =jsonSerializer.serialize(success);
			String jsonString = "{success:"+resultJson.toString()+"}";
			ServletActionContext.getResponse().setContentType("text;charset=utf-8");
			ServletActionContext.getResponse().getWriter().print(jsonString);
		}
	    
		return NONE;
	}
/**
 * 员工职位及系数
 * 
 */
	public String  Listposition() throws Exception{
	   
		List<Position> p=employeeService.ListallPosition();
      
		// 输出json内容到客户端
        JSONSerializer jsonSerializer = new JSONSerializer();
        jsonSerializer.exclude("*.class");
        String resultJson1 = jsonSerializer.deepSerialize(p);  
 
        String jsonString = "{positions:"+resultJson1.toString()+"}";
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
	}
	public void  editPosition() throws Exception{
			
			if(employee.getId()!=0){
		
				employeeService.updatePosition(employee.getId(), coffer);
				this.success=true;
				JSONSerializer jsonSerializer = new JSONSerializer();
				String resultJson =jsonSerializer.serialize(success);
				String jsonString = "{success:"+resultJson.toString()+"}";
				ServletActionContext.getResponse().setContentType("text;charset=utf-8");
				ServletActionContext.getResponse().getWriter().print(jsonString);
			}
			if(employee.getName().equals("院办正职")){
				Employees e=employeeService.findBypositionAndDept(5, 31);
				DepartmentCoffer d=deptFinaceService.FindDepartmentCofferByName(e.getDepartment().getName());
				double olddeptcoffe=d.getCoffe();//科室原奖金系数
				double oldcoffe=e.getFinaceCoffe();//人员原奖金系数
				double changecoffer =coffer-oldcoffe; //改变差额
				e.setFinaceCoffe(coffer);
				employeeService.updateEmployees(e);
				d.setCoffe(olddeptcoffe+changecoffer);
				deptFinaceService.updateDepartmentCoffer(d);
			}if(employee.getName().equals("医管办正职")){
				Employees e=employeeService.findBypositionAndDept(5, 26);
				DepartmentCoffer d=deptFinaceService.FindDepartmentCofferByName(e.getDepartment().getName());
				double olddeptcoffe=d.getCoffe();//科室原奖金系数
				double oldcoffe=e.getFinaceCoffe();//人员原奖金系数
				double changecoffer =coffer-oldcoffe; //改变差额
				e.setFinaceCoffe(coffer);
				employeeService.updateEmployees(e);
				d.setCoffe(olddeptcoffe+changecoffer);
				deptFinaceService.updateDepartmentCoffer(d);
				
			}if(employee.getName().equals("医务科正职")){
				Employees e=employeeService.findBypositionAndDept(5, 31);
				DepartmentCoffer d=deptFinaceService.FindDepartmentCofferByName(e.getDepartment().getName());
				double olddeptcoffe=d.getCoffe();//科室原奖金系数
				double oldcoffe=e.getFinaceCoffe();//人员原奖金系数
				double changecoffer =coffer-oldcoffe; //改变差额
				e.setFinaceCoffe(coffer);
				employeeService.updateEmployees(e);
				d.setCoffe(olddeptcoffe+changecoffer);
				deptFinaceService.updateDepartmentCoffer(d);
			}
			if(employee.getName().equals("保健科正职")){
				Employees e=employeeService.findBypositionAndDept(5, 1);
				DepartmentCoffer d=deptFinaceService.FindDepartmentCofferByName(e.getDepartment().getName());
				double olddeptcoffe=d.getCoffe();//科室原奖金系数
				double oldcoffe=e.getFinaceCoffe();//人员原奖金系数
				double changecoffer =coffer-oldcoffe; //改变差额
				e.setFinaceCoffe(coffer);
				employeeService.updateEmployees(e);
				d.setCoffe(olddeptcoffe+changecoffer);
				deptFinaceService.updateDepartmentCoffer(d); 
				
			}if(employee.getName().equals("院办副职")){
				Employees e=employeeService.findBypositionAndDept(4, 31);
				DepartmentCoffer d=deptFinaceService.FindDepartmentCofferByName(e.getDepartment().getName());
				double olddeptcoffe=d.getCoffe();//科室原奖金系数
				double oldcoffe=e.getFinaceCoffe();//人员原奖金系数
				double changecoffer =coffer-oldcoffe; //改变差额
				e.setFinaceCoffe(coffer);
				employeeService.updateEmployees(e);
				d.setCoffe(olddeptcoffe+changecoffer);
				deptFinaceService.updateDepartmentCoffer(d);
				 
			}if(employee.getName().equals("医管办副职")){
				Employees e=employeeService.findBypositionAndDept(4, 26);
				DepartmentCoffer d=deptFinaceService.FindDepartmentCofferByName(e.getDepartment().getName());
				double olddeptcoffe=d.getCoffe();//科室原奖金系数
				double oldcoffe=e.getFinaceCoffe();//人员原奖金系数
				double changecoffer =coffer-oldcoffe; //改变差额
				e.setFinaceCoffe(coffer);
				employeeService.updateEmployees(e);
				d.setCoffe(olddeptcoffe+changecoffer);
				deptFinaceService.updateDepartmentCoffer(d);
				 
			}if(employee.getName().equals("医务科正职")){
				
			}if(employee.getName().equals("保健副职")){
				Employees e=employeeService.findBypositionAndDept(5, 1);
				DepartmentCoffer d=deptFinaceService.FindDepartmentCofferByName(e.getDepartment().getName());
				double olddeptcoffe=d.getCoffe();//科室原奖金系数
				double oldcoffe=e.getFinaceCoffe();//人员原奖金系数
				double changecoffer =coffer-oldcoffe; //改变差额
				e.setFinaceCoffe(coffer);
				employeeService.updateEmployees(e);
				d.setCoffe(olddeptcoffe+changecoffer);
				deptFinaceService.updateDepartmentCoffer(d); 
				
			}if(employee.getName().equals("一级科室正职")){
				List<Employees>  employees=employeeService.findEmployeesByposition(5);
				for(Employees e:employees){
					if(e.getDepartment().getId()!=31 || e.getDepartment().getId()!=26 ||e.getDepartment().getId()!=31 || e.getDepartment().getId()!=31 ){
						DepartmentCoffer d=deptFinaceService.FindDepartmentCofferByName(e.getDepartment().getName());
						double olddeptcoffe=d.getCoffe();//科室原奖金系数
						double oldcoffe=e.getFinaceCoffe();//人员原奖金系数
						double changecoffer =coffer-oldcoffe; //改变差额
						e.setFinaceCoffe(coffer);
						employeeService.updateEmployees(e);
						d.setCoffe(olddeptcoffe+changecoffer);
						deptFinaceService.updateDepartmentCoffer(d); 
					}
				}
			}if(employee.getName().equals("一级科室副职")){
				
			}if(employee.getName().equals("二级科室正职")){
				
			}if(employee.getName().equals("二级科室副职")){
				
			}
			
			
		    
		}
	public String  ListzhuyuanEmployees() throws Exception{
	    PageRequestBean pageRequestBean = new PageRequestBean();
	    DetachedCriteria criteria = DetachedCriteria.forClass(Employees.class);
	    Employees  existEmployee =(Employees) ServletActionContext.getRequest().getSession().getAttribute("existEmployee");
	    if(!existEmployee.getName().equals("admin")){
	    	criteria.add(Restrictions.eq("department.id",existEmployee.getDepartment().getId()));
	    //	criteria.add(Restrictions.eq("department.xzorzy",1));
	    	pageRequestBean.setDept(existEmployee.getDepartment().getId());
	    }else{
	    	if(serchdepartment!=0){
				criteria.add(Restrictions.eq("department.id",serchdepartment));
				//criteria.add(Restrictions.eq("department.xzorzy",1));
				pageRequestBean.setDept(serchdepartment);
			}
	    }
		
		pageRequestBean.setCriteria(criteria);
		pageRequestBean.setStart(start);
		pageRequestBean.setLimit(limit);
		pageRequestBean.setPage(page);
		PageResponseBean pageResponseBean =employeeService.findEmployee(pageRequestBean);
        employees=pageResponseBean.getRows();
        int total=0;
        total =pageResponseBean.getTotal();
		// 输出json内容到客户端
        JSONSerializer jsonSerializer = new JSONSerializer();
        jsonSerializer.include("*.id","*.name","*.professional","*.department","*.workage","*.education","*.password","*.card","*.zuname");
        jsonSerializer.exclude("*.class");
        String resultJson1 = jsonSerializer.deepSerialize(employees);  
        String resultJson2 =jsonSerializer.serialize(total);
        System.out.println(resultJson2);
        String jsonString = "{total:"+resultJson2.toString()+",employees:"+resultJson1.toString()+"}";
        System.out.print(jsonString);
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
  }
	public String  Listzu() throws Exception{
	
		System.out.println("jinru//////////////////////////////"+deptId);
		Employees a=employeeService.findEmployeesById(deptId);
		employees=employeeService.findListzubydeptid(a.getDepartment().getId());
		JSONSerializer jsonSerializer = new JSONSerializer();
        jsonSerializer.include("*.id","*name");
        jsonSerializer.exclude("*.class");
        String resultJson1 = jsonSerializer.deepSerialize(employees);  
        String jsonString = "{zu:"+resultJson1.toString()+"}";
        System.out.print(jsonString);
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
		
	}
	
	public String addzu() throws Exception{
		System.out.println(empID+"=============="+zu);
		if (zu>0) {
			Employees a=employeeService.findEmployeesById(zu);
			 employeeService.addEMPzu(empID, a.getName());
		}
	   
	 	this.success=true;
		JSONSerializer jsonSerializer = new JSONSerializer();
		String resultJson =jsonSerializer.serialize(success);
		String jsonString = "{success:"+resultJson.toString()+"}";
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
	}
}
	

